<template>
  <div id="center">
    <h1 class="title">个人信息</h1>
    <form>
      <label for="name">
        <span>姓名：</span>
        <input 
          type="text" 
          :disabled="edit" 
          :class="{ 'read': edit, 'error': nameCheck }"
          v-model="name">
      </label>
      <em v-show="nameCheck">*姓名不能为空且只能由字母、数字、下划线组成</em>

      <label for="tel">
        <span>电话号码：</span>
        <input 
          type="text" 
          :disabled="edit" 
          :class="{ 'read': edit, 'error': telCheck }"
          v-model="tel">
      </label>
      <em v-show="telCheck">*不能为空且全部为数字组成</em>

      <label for="pwd">
        <span>密码：</span>
        <input 
          type="text" 
          :disabled="edit" 
          :class="{ 'read': edit, 'error': pwdCheck }"
          v-model="pwd">
      </label>
      <em v-show="pwdCheck">*密码不少于6位且只能由字母、数字、下划线组成</em>
    </form>
    <div class="button_group">
      <div class="editBtn" v-if="edit">
        <button class="large" @click="edit=false">编辑信息</button>
      </div>
      <div class="saveBtn" v-else>
        <button
          :disabled="!save"
          :class="{'normal': !save}"
          @click.prevent.stop="change">
        保存</button>
        <button class="normal" @click="notChange">返回</button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'center',
  props: [ 'personal' ],
  data() {
    return {
      edit:true,
      // nameCheck: false,
      // telCheck: false,
      // pwdCheck: false,
      name: this.personal.user,
      tel: this.personal.tel,
      pwd: this.personal.pwd
    }
  },
  methods: {
    ...mapActions([ 'loginUserChange' ]),
    change() {
      if(this.name&&this.tel&&this.pwd) {
        this.edit = true
        this.loginUserChange({
          user: this.name,
          tel: this.tel,
          pwd: this.pwd
        })
      }
    },
    notChange() {
      this.edit = true
      this.name = this.personal.user
      this.tel = this.personal.tel
      this.pwd = this.personal.pwd
    }
  },
  computed: {
    nameCheck() {
      return /\W/g.test(this.name) || this.name.length < 1
    },
    telCheck() {
      return /\D/g.test(this.tel) || this.tel.length < 1
    },
    pwdCheck() {
      return /\W/g.test(this.pwd) || this.pwd.length < 6
    },
    save() {

      let result = (!this.nameCheck&&!this.telCheck&&!this.pwdCheck)
      return result
    }
  }
}
</script>

<style lang="less" scoped>
#center {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  h1 {
    font-size: 38px;
    margin-top: 40px;
    margin-bottom: 20px;
  }
  @media screen and (max-width: 600px){
    h1 {
      margin-top: 20px;
    }
  }

  form {
    display: flex;
    flex-direction: column;
    padding-top: 20px;
    label {
      display: flex;
      align-items: center;
      margin-bottom: 25px;
      span {
        width: 87px;
        text-align: right;
        font-size: 16px;
        margin-right: 10px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
      }
      input {
        width: 300px;
        height: 40px;
        font-size: 16px;
        padding-left: 10px;
        border-radius: 5px;
        border: 1px solid #ccc;
        background-color: #fff;
      }
      input.read {
        background-color: transparent;
        border: 0;
        text-align: center;
      }
      input.error {
        border: 2px solid red;
      }
      
      @media screen and (max-width: 600px){
        span,
        input {
          font-size: 13px;
        }
        span {
          width: 70px;
        }
        input {
          width: 150px;
          height: 30px;
        }
      }
      @media screen and (max-width: 320px){
        input {
          width: 100px;
        }
      }
    }
    em {
      color: red;
    }
  }

  .button_group {
    button {
      width: 150px;
      height: 40px;
      border: 0;
      border-radius: 5px;
      background-color: #0f88eb;
      color: #fff;
      font-size: 18px;
      margin-right: 5px;
      cursor: pointer;
    }
    button.large {
      width: 300px;
    }
    button.normal {
      background-color: #ccc;
    }
    @media screen and (max-width: 600px){
      button {
        width: 70px;
      }
      button.large {
        width: 150px;
      }
    }
  }
}
</style>
